#define _CRT_SECURE_NO_WARNINGS 1
/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
class Partition {
public:
    ListNode* partition(ListNode* pHead, int x) {
        struct ListNode* lesshead, * lesstail;
        lesshead = lesstail = (struct ListNode*)malloc(sizeof(struct ListNode));
        lesstail->next = NULL;
        struct ListNode* greathead, * greattail;
        greathead = greattail = (struct ListNode*)malloc(sizeof(struct ListNode));
        greattail->next = NULL;
        struct ListNode* cur = pHead;
        while (cur)
        {
            if (cur->val < x)
            {
                lesstail->next = cur;
                lesstail = lesstail->next;
            }
            else
            {
                greattail->next = cur;
                greattail = greattail->next;
            }
            cur = cur->next;
        }
        lesstail->next = greathead->next;
        greattail->next = NULL;
        struct ListNode* head = lesshead->next;
        free(lesshead);
        free(greathead);
        return head;
    }
};

/*
struct ListNode {
    int val;
    struct ListNode *next;
    ListNode(int x) : val(x), next(NULL) {}
};*/
